var WxParse = require('../../wxParse/wxParse.js');
const { getGoodsDetail,addFavGoods,removeFavGoods,goodsSearch, getBanner, getGoodsRecommend, getGoodsPingtuan }=require('../..//http/api.js')

//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    date:'',
    desc:'',
    value: 3,
    show:true,
    showActionsheet: false,
    groups: [
      { text: '示例菜单', value: 1 },
      { text: '示例菜单', value: 2 },
      { text: '负向菜单', type: 'warn', value: 3 }
    ],
    buttons: [{ text: '取消' }, { text: '确定' }],
    movieItem:[
      { movieType: '励志电影', movieList: [{ id: 1001, name: '记忆之夜' }, { id: 1002, name: '记忆之夜2' }, { id: 1003, name: '记忆之夜3' }]},
      { movieType: '悬疑电影', movieList: [{ id: 1001, name: '记忆之夜4' }, { id: 1002, name: '记忆之夜5' },]},
      { movieType: '喜剧电影', movieList: [{ id: 1001, name: '记忆之夜6' }, { id: 1002, name: '记忆之夜7' },]}
    ],
    date: '2016-09-01',
    array: ['美国', '中国', '巴西', '日本'],
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部',
    index:0,
    v:'小明',
    avtarImg:'',
    username:'',
    todolist:['aa','bb'],
    src:'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',
    motto: 'Hello 微信小程序',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    banners:[]
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  getValue(e) {

    console.log('星星获取：',e);

  },
  close: function () {
    this.setData({
      showActionsheet: false
    })
  },
  btnClick(e) {
    console.log(e)
    this.close()
  }
,
  //打开弹框
  openDia() {
   // console.log('openDia:',this.dialog.data.isShow)
   // console.log('openDia:',this.dialog.data.content)
    this.dialog.toggleShow();
  },
  play() {
    console.log('play')
    this.dialog.toggleShow()

  },
  //监听确定按钮触发的回调
  confirm(e) {
    console.log('监听确定',e.detail)
    this.dialog.toggleShow()
  },
  //监听取消按钮触发的回调
  cancel(e) {
    console.log('监听取消', e.detail)
    this.dialog.toggleShow()

  },
  //省市县
  bindRegionChange(e) {

    console.log('bindRegionChange:',e)
    this.setData({
      region:e.detail.value
    })

  },
  //picker
  bindPickerChange(e) {

    console.log('bindPickerChange:',e)
    this.setData({
      index:e.detail.value
    })
  },
  bindDateChange(e) {
    console.log('bindDateChange:',e)
    this.setData({
      date:e.detail.value
    })

  },
  //搜索输入框
  inputEve(e) {

    console.log('输入获取：',e)
    this.setData({
      v:e.detail.value
    })
  },
  //点击软键盘确定，完成，回车键
  addItem() {
    this.data.todolist.push(this.data.v)
    this.setData({
      todolist: this.data.todolist
    });

  },
  //打开弹框
  clickMe() {
    console.log(this.dialog)
    this.dialog.toggleDialog();
  },
  _confirmEv(e) {
    console.log('监听确定事件:',e.detail)
  },
  _cancelEv(e) {

    console.log('监听取消事件:',e)
  },
  //跳转到登录页
  goLogin(e) {
    let {id}=e.currentTarget.dataset;
    console.log('id:',id)
    // wx.navigateTo({
    //   url: `/pages/login/login?id=${id}`,
    // })


    wx.redirectTo({
      url: `/pages/login/login?id=${id}`,
    })

  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },


  onLoad: function () {
  // wx.setStorageSync('name', '1909A')
  //let v=wx.getStorageSync('name')

    wx.setTabBarBadge({
      index: 2,
      text: '10'
    })
    console.log('onLoad-Page')
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
 onShow(e) {
  
  //let keyword=e.detail

   //测试搜索
  //  goodsSearch('毛').then(res=>{
  //    console.log('搜索结果：',res)
  //  })

   //测试banner
   getBanner().then(res=>{
     console.log('获取banner数据：',res)
     this.setData({
       banners:res.data
     })
   })

   //商品推荐接口
  //  getGoodsRecommend().then(res=>{
  //    console.log('商品推荐：',res)
  //  })

   //拼团
  //  getGoodsPingtuan().then(res=>{
  //    console.log('拼团：',res)
  //  })

   //测试商品收藏

  // let goodsId ='395728';  //这是动态获取的，我这里只是模拟写的固定值
   let goodsId ='395723';  //这是动态获取的，我这里只是模拟写的固定值
   let token ='838670df-66d5-4ad3-9858-d2c0a5e6b845' //提前获取的授权tocken,应该是登录授权获取的，不要写死的，我这里只是测试方便
  //  addFavGoods(goodsId,token).then(res=>{
  //    console.log('测试收藏：',res)
  //  })

   removeFavGoods(goodsId, token).then(res=>{
     console.log('删除收藏：',res)
   })






  //   let id=e.detail
   getGoodsDetail(395742).then(res=>{

      console.log('商品详情：',res.data.content)
      this.setData({
        desc: res.data.content
      })

     WxParse.wxParse('article', 'html', this.data.desc, this,5);

    })

  
    
  },
  onHide() {

    console.log('onHide-Page')
  },
  onReady() {
     //页面初次渲染调用
    this.dialog = this.selectComponent("#dialog")
     
  },
  onUnload() {
    console.log('onUnload-Page')
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  ok(){
    console.log('ok')
  },
  hqyhxx(e) {
    console.log('获取用户信息：',e)
    let {avatarUrl,nickName}=e.detail.userInfo;
    this.setData({
      avtarImg: avatarUrl,
      username: nickName
    })
  },
  getFruitList(e) {
    console.log(e)
  }
})




